package com.fish.admin.module.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fish.admin.module.system.domain.dto.SysRoleDTO;
import com.fish.admin.module.system.domain.dto.SysRolePageQueryDTO;
import com.fish.admin.module.system.domain.entity.SysRole;
import com.fish.admin.module.system.domain.vo.SysRoleVO;
import com.fish.admin.module.system.manager.SysRoleManager;
import com.fish.admin.module.system.service.SysRoleService;
import com.fish.admin.module.system.validator.SystemValidator;
import com.fish.common.core.context.UserContextManager;
import com.fish.common.core.domain.vo.ItemVO;
import com.fish.common.core.domain.vo.PageVO;
import com.fish.common.core.enums.EnabledEnum;
import com.fish.common.core.enums.StatusEnum;
import com.fish.common.mybatisplus.util.PageUtils;
import com.fish.common.support.util.EnumUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class SysRoleServiceImpl implements SysRoleService {

    private final SysRoleManager sysRoleManager;

    @Override
    public PageVO<SysRoleVO> page(SysRolePageQueryDTO sysRolePageQueryDTO) {
        IPage<SysRole> page = sysRoleManager.page(sysRolePageQueryDTO);
        return PageUtils.toPage(page, (e -> {
            SysRoleVO v = new SysRoleVO();
            BeanUtils.copyProperties(e, v);
            return v;
        }));
    }

    @Override
    public void save(SysRoleDTO sysRoleDTO) {
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(sysRoleDTO, sysRole);
        sysRole.setStatus(StatusEnum.YES.getValue());
        sysRole.setReadonly(StatusEnum.YES.getValue());
        sysRole.setEnabled(StatusEnum.YES.getValue());
        sysRole.setTenantNo(UserContextManager.getRequiredContext().getTenantNo());
        sysRoleManager.save(sysRole);
    }

    @Override
    public void deleteById(Long id) {
        SystemValidator.validateRole(id);
        sysRoleManager.deleteById(id);
    }

    @Override
    public void updateById(Long id, SysRoleDTO sysRoleDTO) {
        SystemValidator.validateRole(id);
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(sysRoleDTO, sysRole);
        sysRole.setId(id);
        sysRoleManager.updateById(sysRole);
    }

    @Override
    public List<ItemVO> list() {
        List<SysRole> sysRoleList = sysRoleManager.list(EnabledEnum.YES);
        return sysRoleList.stream().map(e -> {
            ItemVO item = new ItemVO();
            item.setValue(String.valueOf(e.getId()));
            item.setLabel(e.getName());
            return item;
        }).collect(Collectors.toList());
    }

    @Override
    public void changeEnabled(Long id, EnabledEnum anEnum) {
        SysRole sysRole = new SysRole();
        sysRole.setId(id);
        sysRole.setEnabled(EnumUtils.getValue(anEnum));
        sysRoleManager.updateById(sysRole);
    }


}
